package com.ddyx.service.impl.user;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ddyx.entity.user.UBrand;
import com.ddyx.mapper.user.UBrandMapper;
import com.ddyx.service.user.UBrandService;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.ddyx.vo.GoodsVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
@Slf4j
public class UBrandServiceImpl implements UBrandService{

	@Resource
	UBrandMapper uBrandMapper;
	
	@Override
	public ResponseForm getBrandList() {
		ResponseForm result = new ResponseForm();
		try {
			List<UBrand> brandList = uBrandMapper.selectAll();
			result.setData(brandList);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}
	@Transactional
	@Override
	public ResponseForm addBrand(RequestForm param) {
		ResponseForm result = new ResponseForm();
			try {
				UBrand bean = new UBrand();
				Map<String, Object> paramMap = (Map<String, Object>) param.getData();
				MyBeanUtils.populate(bean, paramMap);
				uBrandMapper.insertSelective(bean);
				result.setData(bean);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		return result;
	}
	@Transactional
	@Override
	public ResponseForm updateBrand(RequestForm param) {
		ResponseForm result = new ResponseForm();
		try {
			UBrand bean = new UBrand();
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			MyBeanUtils.populate(bean, paramMap);
			uBrandMapper.updateByPrimaryKeySelective(bean);
			//result.setData(bean);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

	@Override
	public ResponseForm getBrandById(RequestForm param) {
		ResponseForm result = new ResponseForm();
		try {
			UBrand bean = new UBrand();
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			MyBeanUtils.populate(bean, paramMap);
			bean=uBrandMapper.selectByPrimaryKey(bean.getBrandId());
			result.setData(bean);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

	@Override
	public ResponseForm getBrandListPage(RequestForm param) {
		ResponseForm result = new ResponseForm();
		try {
			Map paramMap = (Map) param.getData();
			Example example = new Example(UBrand.class);
			example.setOrderByClause("create_time DESC");
//			Criteria criteria=example.createCriteria();
//			criteria.
			com.ddyx.util.Page.parsePage(paramMap);
			Page<Object> page = PageHelper.startPage((Integer)paramMap.get("page"), (Integer)paramMap.get("size"));
			List<UBrand> brandList = uBrandMapper.selectAll();
			result.setData(brandList);
			result.setTotal((int) page.getTotal());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

}

